package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.query.StrategyCatalogQuery;
import cn.wolfcode.wolf2w.repository.StrategyCatalogRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.util.DBHelper;
import cn.wolfcode.wolf2w.vo.CatalogVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class StrategyCatalogServiceImpl implements IStrategyCatalogService {
    @Autowired
    private StrategyCatalogRepository strategyCatalogRepository;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IDestinationService destinationService;

    @Override
    public void save(StrategyCatalog strategyCatalog) {
        strategyCatalogRepository.save(strategyCatalog);
    }

    @Override
    public void update(StrategyCatalog strategyCatalog) {
        strategyCatalogRepository.save(strategyCatalog);
    }

    @Override
    public void delete(String id) {
        strategyCatalogRepository.deleteById(id);
    }

    @Override
    public StrategyCatalog get(String id) {
        return strategyCatalogRepository.findById(id).orElse(null);
    }

    @Override
    public List<StrategyCatalog> listAll() {
        return strategyCatalogRepository.findAll();
    }

    @Override
    public Page<StrategyCatalog> query(StrategyCatalogQuery qo) {
        Query query = new Query();
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        return DBHelper.query(mongoTemplate,query,StrategyCatalog.class, pageable);
    }

    @Override
    public void saveOrUpdate(StrategyCatalog strategyCatalog) {
        // 根据id找name
        Destination destination = destinationService.get(strategyCatalog.getDestId());
        strategyCatalog.setDestName(destination.getName());

        if (StringUtils.hasLength(strategyCatalog.getDestId())){
            this.update(strategyCatalog);
        }else {
            this.save(strategyCatalog);
        }
    }

    @Override
    public List<CatalogVo> getCalalogVo() {
        // 创建空的list集合
        List<CatalogVo> list = new ArrayList<>();
        // 聚合操作
        TypedAggregation<StrategyCatalog> agg = Aggregation.newAggregation(StrategyCatalog.class, Aggregation.group("destName")
                .push("name").as("names")
                .push("id").as("ids"));
        // 转为List<Map>
        List<Map> datas = mongoTemplate.aggregate(agg, Map.class).getMappedResults();
        for (Map data : datas) {
            CatalogVo vo = new CatalogVo();
            vo.setDestName(data.get("_id").toString());
            List<Object> names = (List<Object>)data.get("names");
            List<Object> ids = (List<Object>)data.get("ids");

            List<StrategyCatalog> scList = new ArrayList<>();
            for (int i = 0; i < names.size(); i++){
                StrategyCatalog sc = new StrategyCatalog();
                sc.setId(ids.get(i).toString());
                sc.setName(names.get(i).toString());
                scList.add(sc);
            }
            vo.setMapList(scList);
            list.add(vo);
        }
        return list;
    }

    @Override
    public List<StrategyCatalog> findByDestId(String destId) {
        return strategyCatalogRepository.findByDestId(destId);
    }
}
